package com.qf.sb3.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;

@Controller
public class FileController {
    @Value("${my.base.path}")
    private String basePath;

    @RequestMapping("/show/{file}")
    public void show(@PathVariable("file") String file, HttpServletResponse response){
        response.setHeader("Content-Disposition", "attachment; filename="+ file);
        try (
                FileInputStream inputStream = new FileInputStream(basePath + file);
                final ServletOutputStream outputStream = response.getOutputStream()
        ){
            FileCopyUtils.copy(inputStream, outputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
